(ns ura.table
  (:require
   [reagent.core :as r]
   [reagent.dom :as rd]
   [stylefy.core :as sty]
   [stylefy.reagent :as stylefy-reagent]
   [ajax.core :refer [GET POST DELETE]]
   [ura.common :refer [log]]
   [taoensso.telemere :as t]
   [re-com.core :as rc :refer [at]]

   [ura.style :refer [attr]]
   ))


(defn MyPage [atr & nodes]
 ;; (fn [attr & nodes]
  (let [{
         current :current
         total :total
         pageSize :pageSize
         pageSizeItem :pageSizeItem
         } atr

        pageNum (inc (quot total @pageSize))
        
        _ (log "pageSizeitem: " pageSizeItem)

        ]
    
    [:div (attr :flex :justify-end)
     [:button (attr :.btn :m=0px:10px :.minimal :.small :.accent
                 {:disabled (= 1 @current) :on-click #(swap! current dec)}
                 )  " 上一页 "]
     [:div (attr :flex :mx-2)
      (doall (map (fn [i]
                    [:button.btn (attr :m=0px:5px :.small :.accent
                                (if (= @current i) :.outlined :.minimal)
                                {:key i :on-click #(reset! current i)}) i]
                    )
                  (range 1 (+ pageNum 1)))
             )
      ]
      [rc/single-dropdown {:src (rc/at)
                           :model pageSize
                           :width "80px"
                           :on-change #(reset! pageSize %)
                           :choices (doall (map ( fn [v] {:label v :id v}) pageSizeItem))
                           }]
     [:button.btn (attr :m=0px:10px :.minimal :.small :.accent
             {:disabled (= pageNum @current) :on-click #(swap! current inc)}
             ) " 下一页 "]
     ]
    ))
;;)


(defn TableHeader [cols]
  (log "gen table header: " cols)
    [:thead
     [:tr {:key "hd"} (doall (map-indexed (fn [i, col] [:th {:key i} [:abbr (:title col)]]) cols)) ]
     ]
   
  )

(defn getValue [rowData prop]
  (cond
    (nil? prop) rowData
    (keyword? prop) (or (get rowData prop) (get rowData (name prop)))
    (string? prop) (or (get rowData (keyword prop)) (get rowData prop))
    (or (list? prop) (vector? prop)) (get-in rowData prop) 
    )
  )



(defn TableCell [rowData col ci]
  (let [
        prop (:prop col)
        render (:render col)
        width (:width col)
        value (getValue rowData prop)
        ]
    [:td {:key ci :style {:width width}}
     (cond
       render (render rowData value)
       :else [:span {} value]
       )
     ]
    )
  )

(defn TableRow [i rowData cols]
  [:tr {:key i}
   (doall (map-indexed (fn [colIndex col]
                  (TableCell rowData col colIndex)
                  ) cols))
   ]
  )

(defn MyTable [attr & nodes]
  (log "create table " attr)
  (let[ ;; 这段代码只在初始化时执行1次
       {data :data
        {current :current
         pageSize :pageSize
         pageSizeItem :pageSizeItem
         } :page
        } attr
       
       cur (r/atom current)
       psize (r/atom pageSize)
       ]
    (fn [attr & nodes] ;; fn内的在attr，snode有变化时执行
      (let [
            {
             data :data
             columns :columns
             striped :striped
             bordered :bordered
             hoverable :hoverable
             fullwidth :fullwidth
             } attr
            total (count data)
            
            starti (* @psize (dec @cur))
            endi (min total (+ starti @psize))
            data1 (subvec data starti endi)
            ]
        (log "refresh table , get slice index: " @cur starti endi)
        [:div
         [:table.table {
                        :class (str (when striped " is-striped ")
                                    (when bordered " is-bordered ")
                                    (when hoverable " is-hoverable ")
                                    (when fullwidth " is-fullwidth "))
                        }
          [TableHeader columns]
          [:tbody
           (doall (map-indexed (fn [i, row]
                          (TableRow i row columns)
                          )
                        data1))
           ]

          ]

         [:div
          [MyPage {
                   :total total
                   :current cur
                   :pageSize psize
                   :pageSizeItem pageSizeItem
                   } ]
          ]
         ]
        ))
    )
  )
